package com.demo.sbexample.handle;


import com.demo.sbexample.entity.Result;
import com.demo.sbexample.enums.ResultEnum;
import com.demo.sbexample.exception.SysException;
import com.demo.sbexample.utils.ResultUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


//捕获Controller的异常
@ControllerAdvice
public class ExceptionHandle {
    private static final Logger logger = LoggerFactory.getLogger(ExceptionHandle.class);

    @ExceptionHandler(value = Exception.class)
    @ResponseBody
    public Result handle(Exception e) {
        if (e instanceof SysException) {
            SysException personException = (SysException) e;
            return ResultUtil.error(personException.getCode(), personException.getMessage());
        }
        logger.error("【系统异常】", e);
        return ResultUtil.error(ResultEnum.UNKOWN_ERROR.getCode(), ResultEnum.UNKOWN_ERROR.getMsg());
    }
}
